package com.lujianfei.netease_android_mvvm_kotlin

import android.content.Context
import android.content.Intent
import android.os.SystemClock
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.ActionMenuView
import androidx.appcompat.widget.Toolbar
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.RootMatchers.withDecorView
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule
import com.lujianfei.netease_android_mvvm_kotlin.adapter.MainAdapter
import org.hamcrest.CoreMatchers.not
import org.hamcrest.Matcher
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith


/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
class MainActivityTest {

    // 测试主界面
    @get:Rule
    val mActivityTestRule = ActivityTestRule<MainActivity>(MainActivity::class.java)

    lateinit var appContext:Context

    lateinit var mActivity : MainActivity
    @Before
    fun setup() {
        appContext =
            InstrumentationRegistry.getInstrumentation().targetContext
        mActivity = mActivityTestRule.activity
    }

    @Ignore
    @Test
    fun should_show_toast_when_click_on_item() {
        // 点击 recyclerview 的第1项
        onView(ViewMatchers.withId(R.id.recyclerview))
            .perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(0,click()))
        //会弹出一个文本为 "title 1 被点击" 的Toast
        Thread.sleep(1000);
        onView(withText("title 1 被点击"))
            .inRoot(withDecorView(not(mActivityTestRule.activity.window.decorView)))
            .check(matches(isDisplayed()));
    }
    @Test
    fun list_should_reduce_when_click_on_delete() {
        // 获取当前列表长度
        val listSize = listSize()
        // 打开菜单
        openActionBarOverflowOrOptionsMenu(appContext)
        SystemClock.sleep(1000)
        // 点击编辑
        onView(withText("编辑"))
            .perform(click())
        SystemClock.sleep(1000)
        // 选中第1项
        onView(ViewMatchers.withId(R.id.recyclerview))
            .perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(0,click()))
        // 点击删除
        onView(withId(R.id.delete))
            .perform(click())
        SystemClock.sleep(1000)
        // 验证删除后是否长度 - 1
        assertTrue(listSize() == (listSize - 1))
    }

    private fun listSize(): Int {
        val mAdapterField = mActivity.javaClass.getDeclaredField("mAdapter")
        mAdapterField.isAccessible = true
        val mMainAdapter = mAdapterField.get(mActivity) as MainAdapter
        return mMainAdapter.itemCount
    }
}